﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RoadTeam.Model;
using System.Data;
using RoadTeam.Enums;
using RoadTeam.SysInterface;
using RoadTeam.Pub;

namespace RoadTeam.BLL
{
    public class DictBLL
    {
        private static object o = new object();

        public DataTable GetDictNotSync(String DictType)
        {
            DataTable dt_Dict = FactoryClass.CreatePcDict().GetDict(DictType);
            return dt_Dict;
        }

        public DataTable GetDictSync(String DictType)
        {
            Sync(DictType);
            SqlWhere where = new SqlWhere();
            where.Info = new PcDict() { DictTypeCode = DictType };
            where.SelectColumn = @" DictIdentifier,DictCode,DictName,DictTypeCode ";
            return PcDict.Select(where, GlobalConfig.ConnectionString);
        }

        public void Sync(String DictType)
        {
            SyncUpdate(DictType);
            SyncInSert(DictType);
        }

        public void SyncInSert(String DictType)
        {
            DataTable dt = GetEmptyData();
            DataRow dr;
            lock (o)
            {
                DataTable dt_Rt_Dict = FactoryClass.CreatePcDict().GetDictIdentifierNull(DictType);
                foreach (DataRow dr_Temp in dt_Rt_Dict.Rows)
                {
                    //DictIdentifier,DictCode,DictName,DictTypeCode
                    dr = dt.NewRow();
                    dr_Temp[4] = CreateIdentifier.GetIdentifier();
                    dr["DictIdentifier"] = dr_Temp[4];
                    dr["DictCode"] = dr_Temp[1];
                    dr["DictName"] = dr_Temp[2];
                    dr["DictTypeCode"] = dr_Temp[3];
                    dt.Rows.Add(dr);
                }
                FactoryClass.CreatePcDict().Update(dt_Rt_Dict);
            }
            PcDict.InsertDatatable(dt, GlobalConfig.ConnectionString);
        }

        public void SyncUpdate(String DictType)
        {
            DataTable dt_Rt_Dict = FactoryClass.CreatePcDict().GetDictIdentifierNotNull(DictType);
            String Str = String.Empty;
            foreach (DataRow item in dt_Rt_Dict.Rows)
            {
                Str += "'" + item[4].ToString() + "',";
            }
            if (String.IsNullOrEmpty(Str)) return;
            Str = @" 1=1 And  DictIdentifier IN (" + Str.TrimEnd(',') + ") ";
            DataTable dt = GetDataByIdentity(Str);
            String Rtidentity = @"App_Identifier";
            String idtntity = @"DictIdentifier";
            String EnmunType = @"PcDictColumnDictionary";
            RoadTeam.Pub.SyncTable.SyncDataTable(dt_Rt_Dict, dt, Rtidentity, idtntity, EnmunType);
            PcDict.Update(dt, GlobalConfig.ConnectionString);
        }

        public DataTable GetDataByIdentity(String StrWhere)
        {
            SqlWhere where = new SqlWhere();
            where.StrSqlWhere = StrWhere;
            return PcDict.Select(where, GlobalConfig.ConnectionString);
        }
        public DataTable GetEmptyData()
        {
            SqlWhere where = new SqlWhere();
            where.Info = new PcDict() { DictIdentifier = @"-1" };
            where.SelectColumn = @" DictIdentifier,DictCode,DictName,DictTypeCode ";
            return PcDict.Select(where, GlobalConfig.ConnectionString);
        }
    }
}
